# Carnal Realms Muslim system helpers.

cr_hajj_randomize_land_threshold_effect = {
	clr_character_flag = cr_hajj_threshold_3
	clr_character_flag = cr_hajj_threshold_4
	random_list = {
		50 = { set_character_flag = cr_hajj_threshold_3 }
		50 = { set_character_flag = cr_hajj_threshold_4 }
	}
}

cr_hajj_clear_neglect_effect = {
	clr_character_flag = cr_hajj_moved_this_month
	clr_character_flag = cr_hajj_neglect_warning
	clr_character_flag = cr_hajj_neglect_1
	clr_character_flag = cr_hajj_neglect_2
	clr_character_flag = cr_hajj_neglect_3
	clr_character_flag = cr_hajj_neglect_4
	remove_character_modifier = cr_hajj_neglect_warning_modifier
	remove_character_modifier = cr_hajj_neglect_severe_modifier
}

cr_hajj_suspend_neglect_effect = {
	remove_character_modifier = cr_hajj_neglect_warning_modifier
	remove_character_modifier = cr_hajj_neglect_severe_modifier
}

cr_hajj_clear_journey_effect = {
	clr_character_flag = activated_hajj
	clr_character_flag = preparing_hajj
	clr_character_flag = cr_hajj_active
	clr_character_flag = cr_hajj_outbound
	clr_character_flag = cr_hajj_returning
	clr_character_flag = cr_hajj_ship_hired
	clr_character_flag = cr_hajj_dispatch_land
	clr_character_flag = cr_hajj_dispatch_sea
	clr_character_flag = cr_hajj_ai_journey
	clr_character_flag = cr_hajj_ai_delay
	clr_character_flag = cr_hajj_ai_setback
	clr_character_flag = cr_hajj_ai_failure_1
	cr_hajj_clear_neglect_effect = yes
	set_variable = { which = cr_hajj_province_counter value = 0 }
	set_variable = { which = cr_hajj_ai_progress value = 0 }
	set_variable = { which = cr_hajj_monthly_cost value = 0 }
	remove_character_modifier = cr_hajj_active_modifier
	disband_event_forces = cr_hajj_escort
	disband_event_forces = cr_hajj_ship
	clear_persistent_event_target = cr_hajj_home
	cr_clear_pending_ending_trait_effect = yes
}

cr_hajj_begin_journey_effect = {
	cr_clear_pending_ending_trait_effect = yes
	clr_character_flag = activated_hajj
	clr_character_flag = preparing_hajj
	clr_character_flag = promise_hajj
	set_character_flag = cr_hajj_active
	set_character_flag = cr_hajj_outbound
	clr_character_flag = cr_hajj_returning
	set_variable = { which = cr_hajj_province_counter value = 0 }
	set_variable = { which = cr_hajj_ai_progress value = 0 }
	cr_hajj_randomize_land_threshold_effect = yes
	save_persistent_event_target = {
		name = cr_hajj_home
		scope = capital_scope
	}
	add_character_modifier = {
		name = cr_hajj_active_modifier
		duration = -1
	}
	if = {
		limit = { ai = no }
		spawn_unit = {
			province = capital_scope
			home = capital_scope
			owner = ROOT
			leader = ROOT
			earmark = cr_hajj_escort
			cannot_inherit = yes
			disband_on_peace = no
			attrition = 0
			maintenance_multiplier = 1.0
			reinforces = yes
			reinforce_rate_multiplier = 1.0
			troops = {
				knights = { 100 100 }
			}
		}
	}
	if = {
		limit = { ai = yes }
		set_character_flag = cr_hajj_ai_journey
		set_character_flag = cr_hajj_moved_this_month
		character_event = { id = 86020 days = 25 }
	}
}

cr_hajj_spawn_ship_effect = {
	# AI Hajj travel is simulated; only a player receives a physical fleet.
	if = {
		limit = { ai = no }
		set_character_flag = cr_hajj_ship_hired
		spawn_fleet = {
			province = closest
			owner = ROOT
			earmark = cr_hajj_ship
			cannot_inherit = yes
			disband_on_peace = no
			troops = {
				galleys = { 1 1 }
			}
		}
	}
}

cr_hajj_begin_return_effect = {
	clr_character_flag = cr_hajj_outbound
	set_character_flag = cr_hajj_returning
	set_variable = { which = cr_hajj_province_counter value = 0 }
	set_variable = { which = cr_hajj_ai_progress value = 0 }
	cr_hajj_randomize_land_threshold_effect = yes
	if = {
		limit = { has_character_flag = cr_hajj_ai_journey }
		character_event = { id = 86020 days = 25 }
	}
}

cr_hajj_find_companion_effect = {
	clear_event_target = cr_hajj_companion
	location = {
		random_province_character = {
			limit = { cr_valid_intimacy_partner_trigger = yes }
			save_event_target_as = cr_hajj_companion
		}
	}
}

cr_hajj_resolve_sacred_intimacy_effect = {
	if = {
		limit = { event_target:cr_hajj_companion = { always = yes } }
		event_target:cr_hajj_companion = { save_event_target_as = cr_intimacy_partner }
		cr_resolve_contextual_intimacy_effect = yes
		clear_event_target = cr_intimacy_partner
	}
	set_variable = { which = lust_increase value = 2 }
	character_event = { id = cr_lust.3 }
	decadence = -3
	piety = 25
	prestige = 15
	cr_queue_carnal_good_ending_trait_effect = yes
	clear_event_target = cr_hajj_companion
}

cr_hajj_cost_05_effect = {
	if = {
		limit = { wealth = 1 }
		export_to_variable = { which = cr_hajj_monthly_cost value = wealth }
		multiply_variable = { which = cr_hajj_monthly_cost value = -0.05 }
		wealth = cr_hajj_monthly_cost
	}
	else = { wealth = -1 }
	piety = -25
	prestige = -25
	decadence = 1
}

cr_hajj_cost_10_effect = {
	if = {
		limit = { wealth = 1 }
		export_to_variable = { which = cr_hajj_monthly_cost value = wealth }
		multiply_variable = { which = cr_hajj_monthly_cost value = -0.10 }
		wealth = cr_hajj_monthly_cost
	}
	else = { wealth = -1 }
	piety = -50
	prestige = -50
	decadence = 2
}

cr_hajj_cost_20_effect = {
	if = {
		limit = { wealth = 1 }
		export_to_variable = { which = cr_hajj_monthly_cost value = wealth }
		multiply_variable = { which = cr_hajj_monthly_cost value = -0.20 }
		wealth = cr_hajj_monthly_cost
	}
	else = { wealth = -1 }
	piety = -75
	prestige = -75
	decadence = 3
}

cr_hajj_cost_25_effect = {
	if = {
		limit = { wealth = 1 }
		export_to_variable = { which = cr_hajj_monthly_cost value = wealth }
		multiply_variable = { which = cr_hajj_monthly_cost value = -0.25 }
		wealth = cr_hajj_monthly_cost
	}
	else = { wealth = -1 }
	piety = -100
	prestige = -100
	decadence = 4
}

cr_furusiyya_clear_contestants_effect = {
	if = {
		limit = { event_target:cr_furusiyya_first = { always = yes } }
		event_target:cr_furusiyya_first = { clr_character_flag = cr_furusiyya_selected }
	}
	if = {
		limit = { event_target:cr_furusiyya_second = { always = yes } }
		event_target:cr_furusiyya_second = { clr_character_flag = cr_furusiyya_selected }
	}
	if = {
		limit = { event_target:cr_furusiyya_third = { always = yes } }
		event_target:cr_furusiyya_third = { clr_character_flag = cr_furusiyya_selected }
	}
	clear_event_target = cr_furusiyya_first
	clear_event_target = cr_furusiyya_second
	clear_event_target = cr_furusiyya_third
	clear_event_target = cr_furusiyya_wounded
}

# Shared helpers for the Muslim story decks. The companion is always selected
# through CR's adult intimacy trigger; Hajj draws locally while court stories
# draw from the ruler's court.
cr_muslim_story_find_companion_effect = {
	clear_event_target = cr_muslim_story_companion
	if = {
		limit = { has_character_flag = cr_hajj_active }
		location = {
			random_province_character = {
				limit = { cr_valid_intimacy_partner_trigger = yes }
				save_event_target_as = cr_muslim_story_companion
			}
		}
	}
	else = {
		random_courtier = {
			limit = { cr_valid_intimacy_partner_trigger = yes }
			save_event_target_as = cr_muslim_story_companion
		}
	}
}

cr_muslim_story_resolve_intimacy_effect = {
	if = {
		limit = { event_target:cr_muslim_story_companion = { always = yes } }
		event_target:cr_muslim_story_companion = { save_event_target_as = cr_intimacy_partner }
		cr_resolve_contextual_intimacy_effect = yes
		clear_event_target = cr_intimacy_partner
	}
	piety = 20
	prestige = 15
	decadence = -2
	cr_queue_carnal_good_ending_trait_effect = yes
	clear_event_target = cr_muslim_story_companion
}

cr_muslim_story_diplomacy_test_effect = {
	random_list = {
		65 = {
			modifier = { factor = 1.75 diplomacy = 14 }
			modifier = { factor = 1.30 trait = gregarious }
			modifier = { factor = 0.60 NOT = { diplomacy = 8 } }
			prestige = 25
			piety = 10
			decadence = -1
			cr_queue_diplomacy_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.75 NOT = { diplomacy = 8 } }
			modifier = { factor = 1.30 trait = shy }
			modifier = { factor = 0.60 diplomacy = 14 }
			prestige = -15
			cr_queue_diplomacy_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_martial_test_effect = {
	random_list = {
		65 = {
			modifier = { factor = 1.75 martial = 14 }
			modifier = { factor = 1.30 trait = brave }
			modifier = { factor = 0.60 NOT = { martial = 8 } }
			prestige = 25
			piety = 10
			decadence = -1
			cr_queue_martial_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.75 NOT = { martial = 8 } }
			modifier = { factor = 1.30 trait = craven }
			modifier = { factor = 0.60 martial = 14 }
			prestige = -15
			cr_queue_martial_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_stewardship_test_effect = {
	random_list = {
		65 = {
			modifier = { factor = 1.75 stewardship = 14 }
			modifier = { factor = 1.30 trait = administrator }
			modifier = { factor = 0.60 NOT = { stewardship = 8 } }
			wealth = 15
			prestige = 15
			decadence = -1
			cr_queue_stewardship_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.75 NOT = { stewardship = 8 } }
			modifier = { factor = 1.30 trait = arbitrary }
			modifier = { factor = 0.60 stewardship = 14 }
			wealth = -15
			prestige = -10
			cr_queue_stewardship_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_intrigue_test_effect = {
	random_list = {
		65 = {
			modifier = { factor = 1.75 intrigue = 14 }
			modifier = { factor = 1.30 trait = schemer }
			modifier = { factor = 0.60 NOT = { intrigue = 8 } }
			prestige = 20
			piety = 10
			decadence = -1
			cr_queue_intrigue_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.75 NOT = { intrigue = 8 } }
			modifier = { factor = 1.30 trait = trusting }
			modifier = { factor = 0.60 intrigue = 14 }
			prestige = -15
			decadence = 1
			cr_queue_intrigue_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_learning_test_effect = {
	random_list = {
		65 = {
			modifier = { factor = 1.75 learning = 14 }
			modifier = { factor = 1.30 trait = scholar }
			modifier = { factor = 0.60 NOT = { learning = 8 } }
			piety = 25
			prestige = 10
			decadence = -1
			cr_queue_learning_good_ending_trait_effect = yes
		}
		35 = {
			modifier = { factor = 1.75 NOT = { learning = 8 } }
			modifier = { factor = 1.30 trait = arbitrary }
			modifier = { factor = 0.60 learning = 14 }
			piety = -15
			cr_queue_learning_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_compassion_test_effect = {
	random_list = {
		70 = {
			modifier = { factor = 1.60 trait = kind }
			modifier = { factor = 1.40 trait = charitable }
			modifier = { factor = 1.25 diplomacy = 12 }
			modifier = { factor = 0.50 trait = cruel }
			piety = 25
			prestige = 15
			decadence = -2
			cr_queue_compassion_good_ending_trait_effect = yes
		}
		30 = {
			modifier = { factor = 1.75 trait = cruel }
			modifier = { factor = 1.40 trait = greedy }
			modifier = { factor = 0.50 trait = kind }
			piety = -15
			decadence = 2
			cr_queue_cruel_bad_ending_trait_effect = yes
		}
	}
}

cr_muslim_story_reject_abundance_effect = {
	piety = -20
	prestige = -10
	decadence = 3
	cr_queue_chastity_bad_ending_trait_effect = yes
	clear_event_target = cr_muslim_story_companion
}

cr_muslim_story_continue_hajj_effect = {
	clear_event_target = cr_muslim_story_companion
	long_character_event = { id = 86104 days = 1 }
}

cr_muslim_story_continue_ramadan_effect = {
	clear_event_target = cr_muslim_story_companion
	long_character_event = { id = 88504 days = 2 }
}

cr_muslim_story_continue_furusiyya_effect = {
	clear_event_target = cr_muslim_story_companion
	long_character_event = { id = 88285 days = 2 }
}

cr_muslim_story_continue_fatwa_effect = {
	clear_event_target = cr_muslim_story_companion
	long_character_event = { id = 88750 days = 1 }
}

############################################################
# Hajj failure / Caliphal chastisement
############################################################

cr_hajj_clear_crisis_state_effect = {
	if = {
		limit = { persistent_event_target:cr_hajj_punishment_legate = { always = yes } }
		persistent_event_target:cr_hajj_punishment_legate = {
			disband_event_forces = cr_caliphal_host_1
			disband_event_forces = cr_caliphal_host_2
			disband_event_forces = cr_caliphal_host_3
			disband_event_forces = cr_caliphal_host_4
		}
	}
	clr_character_flag = cr_hajj_crisis
	clr_character_flag = cr_hajj_penance_pending
	clr_character_flag = cr_hajj_ai_failure_1
	remove_character_modifier = cr_hajj_neglect_warning_modifier
	remove_character_modifier = cr_hajj_neglect_severe_modifier
	clear_persistent_event_target = cr_hajj_punishment_legate
}

cr_hajj_end_caliphal_wars_effect = {
	any_war = {
		limit = { using_cb = cr_caliphal_chastisement }
		end_war = invalid
	}
}

cr_hajj_reconcile_effect = {
	cr_hajj_end_caliphal_wars_effect = yes
	cr_hajj_clear_crisis_state_effect = yes
	decadence = -5
	piety = 100
}

cr_hajj_create_chastisement_effect = {
	save_event_target_as = cr_hajj_offender

	if = {
		limit = { persistent_event_target:cr_hajj_punishment_legate = { always = yes } }
		persistent_event_target:cr_hajj_punishment_legate = {
			if = { limit = { is_alive = yes war = no } death = yes }
		}
		clear_persistent_event_target = cr_hajj_punishment_legate
	}

	# The offender's own rightful religious head is the authority.
	if = {
		limit = {
			rightful_religious_head_scope = {
				is_alive = yes
				NOT = { character = ROOT }
			}
		}

		rightful_religious_head_scope = {
			save_event_target_as = cr_hajj_punishment_caliph

			create_character = {
				random_traits = no
				dynasty = none
				religion = ROOT
				culture = ROOT
				female = no
				age = 40
				health = 7
				fertility = 0
				trait = zealous
				trait = brilliant_strategist
				trait = brave
				attributes = {
					diplomacy = 12
					martial = 18
					stewardship = 10
					intrigue = 12
					learning = 15
				}
			}

			new_character = {
				set_character_flag = cr_caliphal_hajj_legate
				save_event_target_as = cr_new_hajj_punishment_legate
				create_title = {
					tier = DUKE
					landless = yes
					temporary = yes
					short_name = yes
					culture = THIS
					name = CR_HAJJ_CALIPHAL_LEGATE_HOST
					ruler = CR_HAJJ_CALIPHAL_LEGATE
					ruler_female = CR_HAJJ_CALIPHAL_LEGATE_FEMALE
					holder = THIS
				}
			}
		}

		save_persistent_event_target = {
			name = cr_hajj_punishment_legate
			scope = event_target:cr_new_hajj_punishment_legate
		}

		event_target:cr_new_hajj_punishment_legate = {
			save_event_target_as = cr_hajj_war_legate

			# Four 10,000-man Caliphal punishment hosts, matching the
			# Catholic and Orthodox enforcement strength.
			spawn_unit = {
				province = event_target:cr_hajj_punishment_caliph
				home = event_target:cr_hajj_punishment_caliph
				owner = THIS
				leader = THIS
				earmark = cr_caliphal_host_1
				cannot_inherit = yes
				disband_on_peace = yes
				attrition = 0.05
				maintenance_multiplier = 0
				reinforces = yes
				reinforce_rate_multiplier = 1.0
				troops = {
					light_infantry = { 2500 2500 }
					heavy_infantry = { 2000 2000 }
					pikemen = { 1000 1000 }
					archers = { 2000 2000 }
					light_cavalry = { 1500 1500 }
					camel_cavalry = { 500 500 }
					horse_archers = { 500 500 }
				}
			}
			spawn_unit = {
				province = event_target:cr_hajj_punishment_caliph
				home = event_target:cr_hajj_punishment_caliph
				owner = THIS
				leader = THIS
				earmark = cr_caliphal_host_2
				cannot_inherit = yes
				disband_on_peace = yes
				attrition = 0.05
				maintenance_multiplier = 0
				reinforces = yes
				reinforce_rate_multiplier = 1.0
				troops = {
					light_infantry = { 2500 2500 }
					heavy_infantry = { 2000 2000 }
					pikemen = { 1000 1000 }
					archers = { 2000 2000 }
					light_cavalry = { 1500 1500 }
					camel_cavalry = { 500 500 }
					horse_archers = { 500 500 }
				}
			}
			spawn_unit = {
				province = event_target:cr_hajj_punishment_caliph
				home = event_target:cr_hajj_punishment_caliph
				owner = THIS
				leader = THIS
				earmark = cr_caliphal_host_3
				cannot_inherit = yes
				disband_on_peace = yes
				attrition = 0.05
				maintenance_multiplier = 0
				reinforces = yes
				reinforce_rate_multiplier = 1.0
				troops = {
					light_infantry = { 2500 2500 }
					heavy_infantry = { 2000 2000 }
					pikemen = { 1000 1000 }
					archers = { 2000 2000 }
					light_cavalry = { 1500 1500 }
					camel_cavalry = { 500 500 }
					horse_archers = { 500 500 }
				}
			}
			spawn_unit = {
				province = event_target:cr_hajj_punishment_caliph
				home = event_target:cr_hajj_punishment_caliph
				owner = THIS
				leader = THIS
				earmark = cr_caliphal_host_4
				cannot_inherit = yes
				disband_on_peace = yes
				attrition = 0.05
				maintenance_multiplier = 0
				reinforces = yes
				reinforce_rate_multiplier = 1.0
				troops = {
					light_infantry = { 2500 2500 }
					heavy_infantry = { 2000 2000 }
					pikemen = { 1000 1000 }
					archers = { 2000 2000 }
					light_cavalry = { 1500 1500 }
					camel_cavalry = { 500 500 }
					horse_archers = { 500 500 }
				}
			}
		}

		unsafe_war = {
			target = event_target:cr_new_hajj_punishment_legate
			casus_belli = cr_caliphal_chastisement
		}

		# The rightful Caliph issues the chastisement and joins his legate.
		event_target:cr_hajj_punishment_caliph = {
			join_defender_wars = event_target:cr_hajj_war_legate
		}

		# Only landed counts and above of the Caliph's religion are summoned.
		any_character = {
			limit = {
				is_alive = yes
				is_ruler = yes
				is_landed = yes
				higher_tier_than = BARON
				religion = event_target:cr_hajj_punishment_caliph
				NOT = { character = ROOT }
				NOT = { character = event_target:cr_hajj_punishment_caliph }
				NOT = { character = event_target:cr_new_hajj_punishment_legate }
			}
			if = {
				limit = { ai = yes }
				join_defender_wars = event_target:cr_hajj_war_legate
			}
			else = {
				long_character_event = { id = cr_hajj_punishment.21 }
			}
		}
	}
}

cr_hajj_start_crisis_effect = {
	cr_hajj_clear_journey_effect = yes
	set_character_flag = cr_hajj_crisis
	decadence = 10
	piety = -250
	cr_hajj_create_chastisement_effect = yes
}

cr_hajj_penance_tithe_effect = {
	scaled_wealth = -0.50
	piety = -250
	decadence = -5
	cr_hajj_reconcile_effect = yes
}

cr_hajj_penance_abasement_effect = {
	prestige = -1000
	piety = -500
	decadence = -5
	if = { limit = { trait = proud } remove_trait = proud }
	if = { limit = { NOT = { trait = humble } } add_trait = humble }
	cr_hajj_reconcile_effect = yes
}

cr_hajj_penance_submission_effect = {
	scaled_wealth = -0.25
	prestige = -500
	piety = -750
	decadence = -10
	cr_hajj_reconcile_effect = yes
}

